package m202406.d05;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/5 13:24
 */
//(C卷,200分)- 任务最优调度
public class C200_01 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String[] tasks = scanner.nextLine().split(",");
        int n = scanner.nextInt();

        System.out.println(handle(tasks, n));
    }

    private static int handle(String[] tasks, int n) {
        Map<String, Integer> map = new HashMap<>();
        for (String task : tasks) {
            map.put(task, map.getOrDefault(task, 0) + 1);
        }

        int max = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            max = Math.max(max, entry.getValue());
        }

        int seam = 0;

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == max) {
                seam++;
            }
        }

        return Math.max(tasks.length, (max - 1) * (n + 1) + seam);
    }

}
